package yf.mall.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.yf.response.RRException;
import sun.misc.REException;
import yf.mall.dao.PriceDao;
import yf.mall.entity.*;
import yf.mall.enums.MallActStatusEnum;
import yf.mall.service.MallActService;
import yf.mall.service.MallTypeService;
import yf.mall.service.PriceService;
import yf.mall.vo.ActGoodsVo;
import yf.mall.vo.PriceVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;

import java.util.Map;

import com.yf.consts.DaoConst;
import yf.mall.service.MallGoodsService;
import com.yf.response.R;


/**
 * 产品Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Mon Aug 05 15:29:09 CST 2019
 */
@RestController
@RequestMapping("/mallGoods")
public class MallGoodsController {
    @Autowired
    private MallGoodsService mallGoodsService;

    @Autowired
    private PriceService priceService;

    /**
     * 列表产品
     */
    @RequestMapping("/list")
    @RequiresPermissions("mall:goods:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageHelper.startPage(DaoConst.getPage(params), DaoConst.getLIMIT(params)).setOrderBy(DaoConst.getOrder(params));
        List<MallGoodsEntity> mallGoodsList = mallGoodsService.queryList(params);
        return R.ok(new PageInfo(mallGoodsList));
    }

    @RequestMapping("/goodsList")
    public R goodsList(){
        List<MallGoodsEntity> mallGoodsEntities = mallGoodsService.queryList(null);

        return R.ok(mallGoodsEntities);
    }
    /**
     * 查看产品
     */
    @SysLog("查看产品")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mall:goods:info")
    public R info(@PathVariable("id") Long id) {
        MallGoodsEntity mallGoods = mallGoodsService.getWithSku(id);
        //获取价格
        List<PriceVo> priceVos = priceDao.findByGoodsId(id);
        List<PriceEntity> priceEntities = new ArrayList<>();
        priceVos.forEach(priceVo -> {
            priceEntities.add(priceVo.getPrice());
        });
        mallGoods.setPrices(priceEntities);
        MallActEntity actEntity = mallActService.findByGoodsIdAndstatus(mallGoods.getId(), MallActStatusEnum.OPEN.getIndex());
        List typesResult = mallTypeService.findByGoodsId(id);

        return R.ok(mallGoods).putExtra("act",actEntity).putExtra("types",typesResult);
    }

    @Autowired
    private PriceDao priceDao;

    @Autowired
    private MallActService mallActService;
    @Autowired
    private MallTypeService mallTypeService;


    /**
     * 保存产品
     */
    @SysLog("保存产品")
    @RequestMapping("/save")
    @RequiresPermissions("mall:goods:save")
    public R save(@RequestBody ActGoodsVo actGoodsVo) {

        MallGoodsEntity goods = actGoodsVo.getGoods();
        mallGoodsService.merge(goods);
        String name = actGoodsVo.getGoods().getName();

        if (mallActService.findByName(name) == null){
            actGoodsVo.getAct().setName(name);
        }
        actGoodsVo.getAct().setGoodsId(goods.getId());
        saveGoodsIdAndTypeId(actGoodsVo.getTypes(),goods.getId());
        mallActService.save(actGoodsVo.getAct()); //保存产品关联活动
        return R.ok(goods);
    }
    /**
     * 修改产品
     */
    @SysLog("修改产品")
    @RequestMapping("/update")
    @RequiresPermissions("mall:goods:update")
    public R update(@RequestBody ActGoodsVo actGoodsVo) {
        if (actGoodsVo.getGoods() == null){
            throw new RRException("产品不存在");
        }
        initAct(actGoodsVo);
        mallActService.update(actGoodsVo.getAct());
        mallGoodsService.merge(actGoodsVo.getGoods());
        return R.ok(actGoodsVo.getGoods());
    }

    /**
     * 删除产品
     */
    @SysLog("删除产品")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:goods:delete")
    public R delete(Long[] ids) {
        mallGoodsService.deleteBatch(ids);
        mallTypeService.delByGoodsIdBatch(ids);
        return R.ok();
    }

    /**
     * 所有产品
     */
    @SysLog("所有产品")
    @RequestMapping("/queryAll")
    public R queryAll(Map<String, Object> params) {

        List<MallGoodsEntity> mallGoodsList = mallGoodsService.queryList(params);

        return R.ok(mallGoodsList);
    }

    /**
     @ModelAttribute public SearchParam<MallGoodsEntity> searchParam(SearchParam<MallGoodsEntity> params, MallGoodsEntity mallGoods) {
     if (mallGoods != null) {
     params.setEntity(mallGoods);
     }

     return params;
     }
     **/
    /***********自定义开始***********/

    @RequestMapping("/stock")
    public R updateStock(StockRecordEntity stockRecordEntity){
        MallGoodsEntity mallGoodsEntity = mallGoodsService.updateStock(stockRecordEntity);
        return R.ok(mallGoodsEntity);
    }
    /**
     * quequnlong
     * 添加产品时添加对应的活动和分类
     *
     * @param actGoodsVo
     */
    private void initAct(ActGoodsVo actGoodsVo) {
        MallActEntity actEntity = actGoodsVo.getAct();
        actEntity.setGoodsId(actGoodsVo.getGoods().getId());

        List<Long> types = actGoodsVo.getTypes();
        //先删分类在添加
        mallTypeService.delByGoodsId(actGoodsVo.getGoods().getId());
        saveGoodsIdAndTypeId(types,actGoodsVo.getGoods().getId());
    }

    /**
     * 添加产品对应的分类
     * @param types
     * @param goodsId
     */
    private void saveGoodsIdAndTypeId(List<Long> types,Long goodsId) {
        if (types != null&&types.size()>0 ){
            for (Long type : types) {
                mallTypeService.saveGoodsIdAndTypeId(goodsId,type);
            }
        }
    }
}
